﻿//////////////////////////////////////////////////////////////////////////////////
//
// Project            : SilverlightAsyncSave
// Module             : SilverlightAsyncSave
// Description        : Main app.
// 
// Repository         : $URL$
// Last changed by    : $LastChangedBy$
// Revision           : $LastChangedRevision$
// Last Changed       : $LastChangedDate$
// Author             : $Author$
//
// Id                 : $Id$
//
// Copyright          : (c) 2010 Torsten Bär
//
// Published under the MIT License. See license.txt or http://www.opensource.org/licenses/mit-license.php.
//
//////////////////////////////////////////////////////////////////////////////////
using System;
using System.Windows;
using System.Windows.Controls;

namespace SilverlightAsyncSave
{
   /// <summary>
   /// The App
   /// </summary>
   public partial class App : Application
   {
      /// <summary>
      /// Initializes a new instance of the <see cref="App"/> class.
      /// </summary>
      public App()
      {
         this.Startup += this.Application_Startup;
         this.UnhandledException += this.Application_UnhandledException;

         InitializeComponent();
      }

      /// <summary>
      /// Handles the Startup event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
      private void Application_Startup(object sender, StartupEventArgs e)
      {
         this.RootVisual = new MainPage();
      }

      /// <summary>
      /// Handles the UnhandledException event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.Windows.ApplicationUnhandledExceptionEventArgs"/> instance containing the event data.</param>
      private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
      {
         // If the app is running outside of the debugger then report the exception using
         // a ChildWindow control.
         if (!System.Diagnostics.Debugger.IsAttached)
         {
            // NOTE: This will allow the application to continue running after an exception has been thrown
            // but not handled. 
            // For production applications this error handling should be replaced with something that will 
            // report the error to the website and stop the application.
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.ExceptionObject);
            errorWin.Show();
         }
      }
   }
}